from time import sleep

from apis.bzy import Bzy


class Order(Bzy):
    def create_order(self, name, phone):
        """
        录入订单
        :param name:收件人姓名
        :param phone:收件人手机
        :return:响应体
        """
        create_url = f"{self.base_url}/v1/wxorderorder"
        json = {"consignee": name, "mobile": phone, "provinceName": "北京市", "cityName": "市辖区",
                "districtName": "朝阳区", "provinceCode": "110000", "cityCode": "110100", "districtCode": "110105",
                "address": "广州街n座", "channelGoodsId": "73202", "channelGoodsName": "你好(规格:哈喽)",
                "goodsNumber": 1, "channelId": "9909322"}

        req = {
            "method": "POST",
            "url": create_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def search_order(self, name):
        sleep(2)
        """
        查询接口
        :param name: 姓名
        :return: 响应体
        """
        search_url = f"{self.base_url}/v1/wxorderorder"
        params = {
            "consignee": name,
        }
        req = {
            "method": "GET",
            "params": params,
            "url": search_url,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def update_take_info(self, new_name, id):
        """
        修改收件信息
        :param new_name: 收件人姓名
        :param id: 要修改的订单id，从search接口中获取
        :return: 响应体
        """
        update_url = f"{self.base_url}/v1/wxorderorder/updateConsigneeInfo/{id}"
        json = {"streetName": "", "consignee": new_name, "mobile": "", "provinceName": "北京市", "cityName": "市辖区",
                "districtName": "朝阳区", "provinceCode": "110000", "cityCode": "110100", "districtCode": "110105",
                "address": ""}

        req = {
            "method": "PUT",
            "url": update_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def update_send_info(self, new_name, id):
        update_url = f"{self.base_url}/v1/wxorderorder/{id}"
        json = {"senderName": new_name, "senderMobile": "15632114481", "senderProvinceName": "北京市",
                "senderCityName": "市辖区", "senderDistrictName": "东城区", "senderAddress": "成都利比里亚"}

        req = {
            "method": "PUT",
            "url": update_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def update_send_fail(self, new_name, id):
        """
        修改收货信息
        :param new_name: 收件人姓名
        :param id: 要修改的订单id，从search接口中获取
        :return: 响应体
        """
        update_url = f"{self.base_url}/v1/wxorderorder/{id}"
        json = {"senderName": new_name, "senderMobile": "", "senderProvinceName": "全部", "senderCityName": "全部",
                "senderDistrictName": "全部", "senderAddress": ""}
        req = {
            "method": "PUT",
            "url": update_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def update_order_money(self, order_money, freight, id):
        """
        修改订单金额
        :param order_money: 订单金额
        :param freight: 运费
        :param id: 订单id
        :return: 响应体
        """
        update_url = f"{self.base_url}/v1/wxorderorder/money"
        json = [{"id": id, "moneyPaid": order_money, "shippingPrice": freight}]
        req = {
            "method": "PUT",
            "url": update_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def void_order(self, order_id, remark):
        """
        作废订单
        :param order_id: 订单id
        :param remark: 作废备注
        :return: 响应体
        """
        void_url = f"{self.base_url}/v1/wxorderorder/bathCancel"
        req = {
            "method": "PUT",
            "url": void_url,
            "json": {"idList": [order_id], "remark": remark},
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def del_order(self, order_id, remark):
        """
        删除订单
        :param order_id: 订单id
        :param remark：删除备注
        :return: 响应体
        """
        del_url = f"{self.base_url}/v1/wxorderorder/realDeleteOrder"
        req = {
            "method": "DELETE",
            "url": del_url,
            "params": {
                "idList": order_id,
                "remark": remark
            },
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r
